<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="net.pws.oos.security.SecurityUtils"%>
<%@ include file="../../header.jsp"%>

<div class="border bg">
<div class="page-header">
	<h2>任务查询<small>(<span id="currentCount">0</span>/<span id="totalCount">0</span>)</small></h2>
	<div class="cms_toolbar">
		<a id="exportButton" href="javascript:;" onclick="exportExcel()" class="btn"><i class="icon-share"></i>导出</a>
	</div>
</div>

<div class="text-right form-inline">
	<label class="control-label" for="locateStartDate">&nbsp;定位从：</label>
	<input type="text" name="locateStartDate" id="locateStartDate" class="input-small" readonly="readonly" value="${locateStartDate}" />
	<label class="control-label" for="locateEndDate">到</label>
	<input type="text" name="locateEndDate" id="locateEndDate" class="input-small" readonly="readonly" value="${locateEndDate}" />
	&nbsp;
	<input type="hidden" name="projectId" id="projectId" value="${projectId}" />
	<input type="text" name="projectName" id="projectName" value="${projectName}" class="input-small" readonly="readonly" onclick="selectProject()" placeholder="请选择项目..."/>
	&nbsp;
	<input type="text" name="executor" id="executor" class="input-small" readonly="readonly" onclick="selectExecutor()" placeholder="请选择部门或人员..."/>
	&nbsp;
	<input type="text" name="name" id="name" class="input-small" placeholder="请输入关键词..."/>
	
	<button class="btn" onclick="refresh()">查询</button>
</div>

<div>
	<br />
	<table id="listTable" class="table table-striped cms-table">
		<thead>
			<tr>
				<th>名称</th>
				<th width="80">项目</th>
				<th width="120">部门</th>
				<th width="50">人员</th>
				<th width="70">定位时间</th>
				<th width="120">勘察地点</th>
				<th width="50">操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
	<div class="well cms_more">
		<a href="javascript:;" onclick="moreData()"><i class="icon-arrow-down"></i>查看更多...</a>
	</div>
</div>

</div>

<table id="taskTemplate" class="hide">
  <tr>
    <td><a href="" target="blank"></a></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td><a href="" class="hide">导出</a></td>
  </tr>
</table>


<script type="text/javascript">
var pageIndex = 0, pageSize = 20, currentCount = 0, totalCount = 0;

function selectProject() {
	$.openDialog("#dialog",
			{height:550,
			width:800,
			minHeight:450,
			minWidth:600,
			loadPage:ctx + "/biz/project/query.do",
			title:"选择项目"},
			function(data) {
				$("#projectId").val(data.id);
				$("#projectName").val(data.name);
			});
}

function exportExcel(){
	if(totalCount == 0) {
		$.alert("您尚未查出数据，无法使用导出功能。");
		return;
	}
	block("#listTable");
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: "excel.json",
    	data: getParameter(),
    	dataType:"json",
    	success:function(json) {
    		unblock("#listTable");
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
			$.info("导出任务已添加，系统处理完成后会通知您，请关注您的新消息。");
    	},
		error: function(message) {
			unblock("#listTable");
			$.alert(message);
		}
	});
}

function getParameter(){
	return {
		start: pageIndex,
		limit: pageSize,
		status: "completed",
		execPersonId: $("#executor").data("type") == "user" ? $("#executor").data("id") : "",
		execOrganId: $("#executor").data("type") == "organ" ? $("#executor").data("id") : "",
		locateStartDate: $("#locateStartDate").val(),
		locateEndDate: $("#locateEndDate").val(),
		projectId: $("#projectId").val(),
		name: $("#name").val(),
		projectName: $("#name").val()
	};
}

function count(){
	block(".cms_more", "正在加载数据...");
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: ctx + "/biz/task/count.json",
    	data: getParameter(),
    	dataType:"json",
    	success:function(json) {
    		unblock(".cms_more");
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
    		totalCount = json.data;
			$("#totalCount").text(json.data);
			list();
    	},
		error: function(message) {
			unblock(".cms_more");
			$.alert(message);
		}
	});
}

function list(){
	block(".cms_more", "正在加载数据...");
	$.ajax({
    	type: "GET",
    	cache: false,
    	url: ctx + "/biz/task/list.json",
    	data: getParameter(),
    	dataType:"json",
    	success:function(json) {
    		unblock(".cms_more");
    		if (!json.succeed) {
    			$.alert(json.message);
    			return;
    		}
			buildList(json.data);
    	},
		error: function(message) {
			unblock(".cms_more");
			$.alert(message);
		}
	});
}

function refresh(){
	pageIndex = 0;
	currentCount = 0;
	totalCount = 0;
	pageSize = 20;
	
	$("#currentCount").text(currentCount);
	$("#totalCount").text(totalCount);
	$("#listTable").find("tbody").html("");
	$(".cms_more").show();
	count();
	//list();
}

function moreData(){
	pageIndex++;
	list();
}

function buildList(data){
	if(data.length == 0){
		$(".cms_more").hide();
		return;
	}
	currentCount += data.length;
	$("#currentCount").text(currentCount);
	if(currentCount >= totalCount){
		$(".cms_more").hide();
	} else {
		$(".cms_more").show();
	}
	$(data).each(function(index, item) {
		buildTd(item).appendTo($("#listTable").find("tbody"));
	});
}

function buildTd(item){
	var taskEle = $("#taskTemplate").clone();
	taskEle.find("tr").attr("id", "item" + item.id);
	var tdList = taskEle.find("td");
	$(tdList.get(0)).find("a").attr("href", ctx + "/biz/task/detail.do?id=" + item.id);
	$(tdList.get(0)).find("a").text(item.name);
	$(tdList.get(1)).text(item.projectName == null ? "" : item.projectName);
	$(tdList.get(2)).text(item.execOrganName);
	$(tdList.get(3)).text(item.execPersonName);
	$(tdList.get(4)).text(item.locateDate == null ? "": item.locateDate);
	$(tdList.get(5)).text(item.locationAddress == null ? "" : item.locationAddress);
	// 操作项
	var operates = $(tdList.get(6)).find("a");
	if(item.status == "completed"){
		$(operates.get(0)).removeClass("hide");
		$(operates.get(0)).attr("href", ctx + "/biz/task/word.do?id=" + item.id);
	}
	
	return taskEle.find("tr");
}

function selectExecutor() {
	$.openDialog("#dialog",
			{height:350,
			width:300,
			minHeight:350,
			minWidth:300,
			loadPage:ctx + "/biz/system/user/tree_single.jsp",
			title:"选择部门或人员"},
			function(data) {
				$("#executor").data("id",data.data("id"));
				$("#executor").data("type",data.data("type"));
				$("#executor").val(data.data("name"));
			});
}


$(document).ready(function() {
	//$("#taskPlanStartDate").datepicker();
	$("#locateStartDate").datepicker();
	$("#locateEndDate").datepicker();
	
	refresh();
});
</script>

<%@ include file="../../footer.jsp"%>